//
//  QQNewAddressVC.m
//  QQZF
//
//  Created by Henry on 2018/7/3.
//  Copyright © 2018年 com.szqqkj. All rights reserved.
//

#import "QQNewAddressVC.h"
#import "HGAddressModel.h"
#import "QQAddressVC.h"

@interface QQNewAddressVC ()<UITextFieldDelegate>

/* <#type#> **/
@property (nonatomic, copy) NSArray<NSDictionary *> *defaultArr;

@property (nonatomic, copy) NSMutableArray<UITextField *> *textFArr;

@property (nonatomic,strong)UIButton *saveAddressBtn;

@end

@implementation QQNewAddressVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initSubViews];
}

- (void)initSubViews {
    
    self.title = @"新增地址";
    
    [self.view setBackgroundColor:kColorBackground];
    
    [self.defaultArr enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *bgView = [[UIView alloc] init];
        bgView.backgroundColor = [UIColor whiteColor];
        
        UILabel *leftLable = [[UILabel alloc] init];
        leftLable.text = obj.allKeys.firstObject;
        
        UITextField *textField = [[UITextField alloc] init];
        textField.placeholder = obj.allValues.firstObject;
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = kColorBackground;
        
        [bgView addSubview:lineView];
        [self.view addSubview:bgView];
        [bgView addSubview:leftLable];
        [bgView addSubview:textField];
        
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.height.offset(60);
            make.top.offset(kNavigationHeight + (idx * 60));
        }];
        
        [leftLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(bgView);
            make.left.offset(15);
        }];
        
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bgView).offset(100);
            make.centerY.equalTo(leftLable);
        }];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(leftLable);
            make.right.bottom.equalTo(bgView);
            make.height.offset(1);
        }];
        
        [self.textFArr addObject:textField];
        
        if (idx == 2) {
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bj_more"]];
            [bgView addSubview:imageView];
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(leftLable);
                make.right.offset(-15);
            }];
            textField.tag = 1;
            textField.delegate = self;
        }
    }];

    [self.saveAddressBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.offset(45);
        make.bottom.offset(-kTabbarSafeBottomMargin);
    }];
    
   if (_model == nil) return;
   NSString *regionStr = [NSString stringWithFormat:@"%@%@%@",_model.province,_model.city,_model.area];
    NSArray *infoMationArr = @[_model.receivePerson,_model.receiveTell,regionStr,_model.detail];
    [_textFArr enumerateObjectsUsingBlock:^(UITextField * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.text = infoMationArr[idx];
    }];
}

#pragma mark - UITextFieldDelegate 返回键
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    if (textField.tag == 1) {
        [self.view endEditing:YES];
        [self handlerTextFieldSelect:textField];
        return NO;
    }
    return YES;
}

#pragma mark - 调用地址选择器 本地 有时间改成线上
- (void)handlerTextFieldSelect:(UITextField *)textField {
    
    QQAddressVC *vc = [QQAddressVC new];
    vc.returnData = ^(NSString *city, NSString *cityS) {
        textField.text = city;
    };
    vc.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:vc animated:YES completion:nil];
}


- (void)newAddress {
    NSString *url = (_model == nil) ? @"app/user/address/add" : @"app/user/address/modify";
    
    __block BOOL isPerfect = YES;
    [_textFArr enumerateObjectsUsingBlock:^(UITextField * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.text.length == 0) isPerfect = NO;
    }];
    
    if (isPerfect == NO) {
        [[XNProgressHUD shared] showErrorWithTitle:@"信息未完善"];
        return;
    }
    
    NSArray *cityS = _model == nil ? [_textFArr[2].text componentsSeparatedByString:@" "]
    :  @[_model.province,_model.city,_model.area];
    
    NSDictionary *parameters = @{
                                 @"receivePerson":_textFArr.firstObject.text,
                                 @"receiveTell":_textFArr[1].text,
                                 @"province":cityS[0],
                                 @"city":cityS[1],
                                 @"area":cityS[2],
                                 @"detail" : _textFArr.lastObject.text,
                                 @"isDefault" : @1,
                                 @"id" : [NSNumber numberWithInteger:_model.addressID]
                                 };
    
    [[LMJRequestManager sharedManager] POST:KBaseURL(url) parameters:parameters completion:^(LMJBaseResponse *response) {
        if(response.responseModel.status == 1) {
            
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

- (UIButton *)saveAddressBtn {
    if (!_saveAddressBtn) {
        _saveAddressBtn = [[UIButton alloc] init];
        [_saveAddressBtn setBackgroundColor:kColorMainTheme];
        [_saveAddressBtn setTitle:@"保存" forState:(UIControlStateNormal)];
        [_saveAddressBtn addTarget:self action:@selector(newAddress) forControlEvents:(UIControlEventTouchUpInside)];
        [self.view addSubview:_saveAddressBtn];
    }
    return _saveAddressBtn;
}

- (NSArray *)defaultArr {
    if (!_defaultArr) {
        _defaultArr = @[
                        @{@"收货人" : @"请输入收货人姓名"},
                        @{@"手机号码" : @"请输入收货人号码"},
                        @{@"所在地区" : @"请选择收货地址"},
                        @{@"详细地址" : @"请输入您的详细地址"}
                        ];
    }
    return _defaultArr;
}

- (NSMutableArray<UITextField *> *)textFArr {
    if (!_textFArr) {
        _textFArr = [@[] mutableCopy];
    }
    return _textFArr;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // 按钮颜色
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    //标题颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
    // 导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    // 按钮颜色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    //标题颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    // 导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
}


@end
